package com.btpn.custody.web.m002.action;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import com.btpn.custody.bean.ValidationProduct;
import com.btpn.custody.common.Constant;
import com.btpn.custody.dao.IAddress;
import com.btpn.custody.dao.IMasterData;
import com.btpn.custody.entity.Address;
import com.btpn.custody.entity.MasterData;
import com.btpn.custody.web.m002.model.AllAddress;
import com.btpn.infra.utils.MyDateUtils;
import com.btpn.infra.utils.MyNumberUtils;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value="custody")
public class IndividuAction extends ActionSupport {

	/*=============================================== C O N S T A N T =========================================*/
	private static final long serialVersionUID = -558727909973335031L;
	
	/*=============================================== M E M B E R =============================================*/		
	@Autowired
	private IAddress addressDaoHibernate;
	
	@Autowired
	private IMasterData masterDataDaoHibernate;
		
	@Autowired
	private ValidationProduct validationProduct;
			
	private String masterSid;
//	private String CIF;					//disable
	private String CUST_FULL_NAME;		//*mandatory
	private String PARENT_COUNTRY;
	private String GENDER;				//*
	private String ID_TYPE;				//*
	private String ID_NUMBER;			//*
	private String ID_EXPIRED;			//*
	private String BIRTH_DATE;			//*
	private String BIRTH_PLACE;
	private String NPWP;
	private String RELIGION;			//*
	private String EDUCATION;
	private String CURRENT_INCOME;		//*
	private String SOURCE_INCOME;		//*
	private String ADDITIONAL_INCOME;
	private String PURPOSE;				//*
	private String PROFESSION;			//*
	private String JABATAN;				//*
	private String JENIS_USAHA;
	private String MARITAL_STATUS;		//*
	private String MOTHER_NAME;			//*
	private String LBU_GOL_NASABAH;		//*
	private String SALDO_NORMAL;
	private String FREQ_TRX_DB;			//*
	private String FREQ_TRX_CR;			//*
	private String EST_TRX_NORMAL_DB;	//*
	private String EST_TRX_NORMAL_CR;	//*
	private String NAMA_AHLI_WARIS;
	private String ALAMAT_AHLI_WARIS1;
	private String ALAMAT_AHLI_WARIS2;
	private String KOTA_AHLI_WARIS;
	private String HUB_AHLI_WARIS;
	private String NOREK_BANK_LAIN1;
	private String NOREK_BANK_LAIN2;
	private String NOREK_BANK_LAIN3;
	private String HR_CUSTOMER;
	private String HR_BUSINESS;
	private String HR_COUNTRIES;
	private String HR_PRODUCT;
	
	private AllAddress destAddress;
	
	/*=============================================== B E H A V I O R =========================================*/
	/*
	 * validate a product individu
	 * next : make object(product) to get the value, then move this bussiness to service
	 */
	@Action(value = "/validateIndividu", results = { 
		@Result(name = "success", type="redirectAction", params = {"actionName", "checklist"} )
	})
	public String validateIndividu() {

		// get MasterData & update
		MasterData md = masterDataDaoHibernate.get(masterSid);
		md.setUpdatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
		md.setUpdatedDate(new Date());
//		masterDataDaoHibernate.update(md);
		
//		Long cifId = md.getCIF().getId();
		
		// update CIF
//		CIF cif = cifDaoHibernate.get(cifId);
		md.getCIF().setCUST_FULL_NAME(CUST_FULL_NAME);
		md.getCIF().setPARENT_COUNTRY(PARENT_COUNTRY);
		md.getCIF().setGENDER(GENDER);
		md.getCIF().setID_TYPE(ID_TYPE);
		md.getCIF().setID_NUMBER(ID_NUMBER);
		md.getCIF().setID_EXPIRED(MyDateUtils.strToDate(Constant.FORMAT_PRESENTATION, ID_EXPIRED));
		md.getCIF().setBIRTH_DATE(MyDateUtils.strToDate(Constant.FORMAT_PRESENTATION, BIRTH_DATE));
		md.getCIF().setPLACE_OF_BIRTH(BIRTH_PLACE);
		md.getCIF().setNPWP(NPWP);
		md.getCIF().setRELIGION(RELIGION);
		md.getCIF().setEDUCATION(EDUCATION);
		md.getCIF().setCURRENT_INCOME(CURRENT_INCOME);
		md.getCIF().setSOURCE_INCOME(SOURCE_INCOME);
		md.getCIF().setADDITIONAL_INCOME(ADDITIONAL_INCOME);
		md.getCIF().setPURPOSE(PURPOSE);
		md.getCIF().setPROFESSION(PROFESSION);
		md.getCIF().setJABATAN(JABATAN);
		md.getCIF().setJENIS_USAHA(JENIS_USAHA);
		md.getCIF().setMARITAL_STATUS(MARITAL_STATUS);
		md.getCIF().setMOTHER_NAME(MOTHER_NAME);
		md.getCIF().setLBU_GOL_NASABAH(LBU_GOL_NASABAH);
		md.getCIF().setSALDO_NORMAL(MyNumberUtils.decideNumeric(SALDO_NORMAL));
		md.getCIF().setFREQ_TRX_DB(MyNumberUtils.decideNumeric(FREQ_TRX_DB));
		md.getCIF().setFREQ_TRX_CR(MyNumberUtils.decideNumeric(FREQ_TRX_CR));
		md.getCIF().setEST_TRX_NORMAL_DB(MyNumberUtils.decideNumeric(EST_TRX_NORMAL_DB));
		md.getCIF().setEST_TRX_NORMAL_CR(MyNumberUtils.decideNumeric(EST_TRX_NORMAL_CR));
		md.getCIF().setNAMA_AHLI_WARIS(NAMA_AHLI_WARIS);
		md.getCIF().setALAMAT_AHLI_WARIS1(ALAMAT_AHLI_WARIS1);
		md.getCIF().setALAMAT_AHLI_WARIS2(ALAMAT_AHLI_WARIS2);
		md.getCIF().setKOTA_AHLI_WARIS(KOTA_AHLI_WARIS);
		md.getCIF().setHUB_AHLI_WARIS(HUB_AHLI_WARIS);
		md.getCIF().setREK_BANK_LAIN1(NOREK_BANK_LAIN1);
		md.getCIF().setREK_BANK_LAIN2(NOREK_BANK_LAIN2);
		md.getCIF().setREK_BANK_LAIN3(NOREK_BANK_LAIN3);
		md.getCIF().setHR_CUSTOMER(HR_CUSTOMER);
		md.getCIF().setHR_BUSINESS(HR_BUSINESS);
		md.getCIF().setHR_COUNTRIES(HR_COUNTRIES);
		md.getCIF().setHR_PRODUCT(HR_PRODUCT);		
//		cifDaoHibernate.update(cif);
		masterDataDaoHibernate.update(md);
		
		// validasi fisrt collumn
		if (StringUtils.trimToNull(destAddress.getAddress31()) != null) {
			Address a3 = addressDaoHibernate.getByMD_AddressType(md.getSid(), Address.PRIME_ADDRESS);
			if (a3==null) {
				a3 = new Address();
				a3.setCreatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
				a3.setCreatedDate(new Date());
				a3.setADDRESS_TYPE(Address.PRIME_ADDRESS);
				a3.setMasterData(md);
				addressDaoHibernate.create(a3);
			}
			a3.setADDRESS1(destAddress.getAddress31());
			a3.setADDRESS2(destAddress.getAddress32());
			a3.setADDRESS3(destAddress.getAddress33());
			a3.setADDRESS4(destAddress.getAddress34());
			a3.setADDRESS5(destAddress.getAddress35());
			a3.setPOST_CODE(destAddress.getPostCode3());
			a3.setLastUpdatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
			a3.setLastUpdatedDate(new Date());
			addressDaoHibernate.update(a3);
		}
		
		// validasi fisrt collumn
		if (StringUtils.trimToNull(destAddress.getAddress11()) != null) {
			Address a1 = addressDaoHibernate.getByMD_AddressType(md.getSid(), Address.HOME_ADDRESS);
			if (a1==null) {
				a1 = new Address();
				a1.setCreatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
				a1.setCreatedDate(new Date());
				a1.setADDRESS_TYPE(Address.HOME_ADDRESS);
				a1.setMasterData(md);
				addressDaoHibernate.create(a1);
			}
			a1.setADDRESS1(destAddress.getAddress11());
			a1.setADDRESS2(destAddress.getAddress12());
			a1.setADDRESS3(destAddress.getAddress13());
			a1.setADDRESS4(destAddress.getAddress14());
			a1.setADDRESS5(destAddress.getAddress15());
			a1.setPOST_CODE(destAddress.getPostCode1());
			a1.setLastUpdatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
			a1.setLastUpdatedDate(new Date());
			addressDaoHibernate.update(a1);
		}
		
		// validasi fisrt collumn
		if (StringUtils.trimToNull(destAddress.getAddress21()) != null) {
			Address a2 = addressDaoHibernate.getByMD_AddressType(md.getSid(), Address.OFFICE_ADDRESS);
			if (a2==null) {
				a2 = new Address();
				a2.setCreatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
				a2.setCreatedDate(new Date());
				a2.setADDRESS_TYPE(Address.OFFICE_ADDRESS);
				a2.setMasterData(md);
				addressDaoHibernate.create(a2);
			}
			a2.setADDRESS1(destAddress.getAddress21());
			a2.setADDRESS2(destAddress.getAddress22());
			a2.setADDRESS3(destAddress.getAddress23());
			a2.setADDRESS4(destAddress.getAddress24());
			a2.setADDRESS5(destAddress.getAddress25());
			a2.setPOST_CODE(destAddress.getPostCode2());
			a2.setLastUpdatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
			a2.setLastUpdatedDate(new Date());
			addressDaoHibernate.update(a2);	
		}
		
		// validasi fisrt collumn
		if (StringUtils.trimToNull(destAddress.getAddress81()) != null) {
			Address a8 = addressDaoHibernate.getByMD_AddressType(md.getSid(), Address.EMAILING_ADDRESS);
			if (a8==null) {
				a8 = new Address();
				a8.setCreatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
				a8.setCreatedDate(new Date());
				a8.setADDRESS_TYPE(Address.EMAILING_ADDRESS);
				a8.setMasterData(md);
				addressDaoHibernate.create(a8);
			}
			a8.setADDRESS1(destAddress.getAddress81());
			a8.setADDRESS2(destAddress.getAddress82());
			a8.setADDRESS3(destAddress.getAddress83());
			a8.setADDRESS4(destAddress.getAddress84());
			a8.setADDRESS5(destAddress.getAddress85());
			a8.setPOST_CODE(destAddress.getPostCode8());
			a8.setLastUpdatedBy(SecurityContextHolder.getContext().getAuthentication().getName());
			a8.setLastUpdatedDate(new Date());
			addressDaoHibernate.update(a8);
		}
		
		// validate a product by CIF
		addActionMessage(validationProduct.validateProduct(md));
	
		return SUCCESS;
	}
	
	/*=============================================== G E T T E R   &   S E T T E R ===========================*/
	public String getMasterSid() {
		return masterSid;
	}

	public void setMasterSid(String masterSid) {
		this.masterSid = masterSid;
	}

	public AllAddress getDestAddress() {
		return destAddress;
	}

	public void setDestAddress(AllAddress destAddress) {
		this.destAddress = destAddress;
	}

	public String getCUST_FULL_NAME() {
		return CUST_FULL_NAME;
	}

	public void setCUST_FULL_NAME(String cUST_FULL_NAME) {
		CUST_FULL_NAME = cUST_FULL_NAME;
	}

	public String getPARENT_COUNTRY() {
		return PARENT_COUNTRY;
	}

	public void setPARENT_COUNTRY(String pARENT_COUNTRY) {
		PARENT_COUNTRY = pARENT_COUNTRY;
	}

	public String getGENDER() {
		return GENDER;
	}

	public void setGENDER(String gENDER) {
		GENDER = gENDER;
	}

	public String getID_TYPE() {
		return ID_TYPE;
	}

	public void setID_TYPE(String iD_TYPE) {
		ID_TYPE = iD_TYPE;
	}

	public String getID_NUMBER() {
		return ID_NUMBER;
	}

	public void setID_NUMBER(String iD_NUMBER) {
		ID_NUMBER = iD_NUMBER;
	}

	public String getID_EXPIRED() {
		return ID_EXPIRED;
	}

	public void setID_EXPIRED(String iD_EXPIRED) {
		ID_EXPIRED = iD_EXPIRED;
	}

	public String getBIRTH_DATE() {
		return BIRTH_DATE;
	}

	public void setBIRTH_DATE(String bIRTH_DATE) {
		BIRTH_DATE = bIRTH_DATE;
	}

	public String getBIRTH_PLACE() {
		return BIRTH_PLACE;
	}

	public void setBIRTH_PLACE(String bIRTH_PLACE) {
		BIRTH_PLACE = bIRTH_PLACE;
	}

	public String getNPWP() {
		return NPWP;
	}

	public void setNPWP(String nPWP) {
		NPWP = nPWP;
	}

	public String getRELIGION() {
		return RELIGION;
	}

	public void setRELIGION(String rELIGION) {
		RELIGION = rELIGION;
	}

	public String getEDUCATION() {
		return EDUCATION;
	}

	public void setEDUCATION(String eDUCATION) {
		EDUCATION = eDUCATION;
	}

	public String getCURRENT_INCOME() {
		return CURRENT_INCOME;
	}

	public void setCURRENT_INCOME(String cURRENT_INCOME) {
		CURRENT_INCOME = cURRENT_INCOME;
	}

	public String getSOURCE_INCOME() {
		return SOURCE_INCOME;
	}

	public void setSOURCE_INCOME(String sOURCE_INCOME) {
		SOURCE_INCOME = sOURCE_INCOME;
	}

	public String getPURPOSE() {
		return PURPOSE;
	}

	public void setPURPOSE(String pURPOSE) {
		PURPOSE = pURPOSE;
	}

	public String getPROFESSION() {
		return PROFESSION;
	}

	public void setPROFESSION(String pROFESSION) {
		PROFESSION = pROFESSION;
	}

	public String getJABATAN() {
		return JABATAN;
	}

	public void setJABATAN(String jABATAN) {
		JABATAN = jABATAN;
	}

	public String getFREQ_TRX_DB() {
		return FREQ_TRX_DB;
	}

	public void setFREQ_TRX_DB(String fREQ_TRX_DB) {
		FREQ_TRX_DB = fREQ_TRX_DB;
	}

	public String getJENIS_USAHA() {
		return JENIS_USAHA;
	}

	public void setJENIS_USAHA(String jENIS_USAHA) {
		JENIS_USAHA = jENIS_USAHA;
	}

	public String getFREQ_TRX_CR() {
		return FREQ_TRX_CR;
	}

	public void setFREQ_TRX_CR(String fREQ_TRX_CR) {
		FREQ_TRX_CR = fREQ_TRX_CR;
	}

	public String getMARITAL_STATUS() {
		return MARITAL_STATUS;
	}

	public void setMARITAL_STATUS(String mARITAL_STATUS) {
		MARITAL_STATUS = mARITAL_STATUS;
	}

	public String getEST_TRX_NORMAL_DB() {
		return EST_TRX_NORMAL_DB;
	}

	public void setEST_TRX_NORMAL_DB(String eST_TRX_NORMAL_DB) {
		EST_TRX_NORMAL_DB = eST_TRX_NORMAL_DB;
	}

	public String getNAMA_AHLI_WARIS() {
		return NAMA_AHLI_WARIS;
	}

	public void setNAMA_AHLI_WARIS(String nAMA_AHLI_WARIS) {
		NAMA_AHLI_WARIS = nAMA_AHLI_WARIS;
	}

	public String getALAMAT_AHLI_WARIS1() {
		return ALAMAT_AHLI_WARIS1;
	}

	public void setALAMAT_AHLI_WARIS1(String aLAMAT_AHLI_WARIS1) {
		ALAMAT_AHLI_WARIS1 = aLAMAT_AHLI_WARIS1;
	}

	public String getALAMAT_AHLI_WARIS2() {
		return ALAMAT_AHLI_WARIS2;
	}

	public void setALAMAT_AHLI_WARIS2(String aLAMAT_AHLI_WARIS2) {
		ALAMAT_AHLI_WARIS2 = aLAMAT_AHLI_WARIS2;
	}

	public String getKOTA_AHLI_WARIS() {
		return KOTA_AHLI_WARIS;
	}

	public void setKOTA_AHLI_WARIS(String kOTA_AHLI_WARIS) {
		KOTA_AHLI_WARIS = kOTA_AHLI_WARIS;
	}

	public String getHUB_AHLI_WARIS() {
		return HUB_AHLI_WARIS;
	}

	public void setHUB_AHLI_WARIS(String hUB_AHLI_WARIS) {
		HUB_AHLI_WARIS = hUB_AHLI_WARIS;
	}

	public String getNOREK_BANK_LAIN1() {
		return NOREK_BANK_LAIN1;
	}

	public void setNOREK_BANK_LAIN1(String nOREK_BANK_LAIN1) {
		NOREK_BANK_LAIN1 = nOREK_BANK_LAIN1;
	}

	public String getNOREK_BANK_LAIN2() {
		return NOREK_BANK_LAIN2;
	}

	public void setNOREK_BANK_LAIN2(String nOREK_BANK_LAIN2) {
		NOREK_BANK_LAIN2 = nOREK_BANK_LAIN2;
	}

	public String getNOREK_BANK_LAIN3() {
		return NOREK_BANK_LAIN3;
	}

	public void setNOREK_BANK_LAIN3(String nOREK_BANK_LAIN3) {
		NOREK_BANK_LAIN3 = nOREK_BANK_LAIN3;
	}

	public String getSALDO_NORMAL() {
		return SALDO_NORMAL;
	}

	public void setSALDO_NORMAL(String sALDO_NORMAL) {
		SALDO_NORMAL = sALDO_NORMAL;
	}

	public String getMOTHER_NAME() {
		return MOTHER_NAME;
	}

	public void setMOTHER_NAME(String mOTHER_NAME) {
		MOTHER_NAME = mOTHER_NAME;
	}

	public String getEST_TRX_NORMAL_CR() {
		return EST_TRX_NORMAL_CR;
	}

	public void setEST_TRX_NORMAL_CR(String eST_TRX_NORMAL_CR) {
		EST_TRX_NORMAL_CR = eST_TRX_NORMAL_CR;
	}

	public String getLBU_GOL_NASABAH() {
		return LBU_GOL_NASABAH;
	}

	public void setLBU_GOL_NASABAH(String lBU_GOL_NASABAH) {
		LBU_GOL_NASABAH = lBU_GOL_NASABAH;
	}

	public String getHR_CUSTOMER() {
		return HR_CUSTOMER;
	}

	public void setHR_CUSTOMER(String hR_CUSTOMER) {
		HR_CUSTOMER = hR_CUSTOMER;
	}

	public String getHR_BUSINESS() {
		return HR_BUSINESS;
	}

	public void setHR_BUSINESS(String hR_BUSINESS) {
		HR_BUSINESS = hR_BUSINESS;
	}

	public String getHR_COUNTRIES() {
		return HR_COUNTRIES;
	}

	public void setHR_COUNTRIES(String hR_COUNTRIES) {
		HR_COUNTRIES = hR_COUNTRIES;
	}

	public String getHR_PRODUCT() {
		return HR_PRODUCT;
	}

	public void setHR_PRODUCT(String hR_PRODUCT) {
		HR_PRODUCT = hR_PRODUCT;
	}

	public String getADDITIONAL_INCOME() {
		return ADDITIONAL_INCOME;
	}

	public void setADDITIONAL_INCOME(String aDDITIONAL_INCOME) {
		ADDITIONAL_INCOME = aDDITIONAL_INCOME;
	}
}
